package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.example.entity.Mycoupon;
import com.example.entity.Users;
import com.example.service.IMycouponService;
import com.example.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;

import java.util.List;

@RequestMapping
@Controller
public class LoginController {



    @Autowired
    private IMycouponService mycouponService;

    @Autowired
    private IUsersService usersService;

    @Autowired
    private thymeleafController thymeleafController;



    @GetMapping("/logup")
    public String hello(Model model) {


    return "loginPage";

    }






        @PostMapping("/login")
    public String Login(@RequestParam("username") String username,@RequestParam("password") String password,Model model){

        Users users = new Users();
        users.setUsername(username);
        users.setPassword(password);



        Users foundUser = usersService.getByUsernameAndPassword(username, password);




        // 判断登录是否成功
        if (foundUser != null) {
            // 登录成功，可以做进一步操作，比如设置登录状态等

            List<Mycoupon> coupons = mycouponService.list(); // 获取所有优惠券列表
            model.addAttribute("coupons", coupons); // 将优惠券列表添加到模型中
            return "show"; // 返回模板名称，Thymeleaf 将渲染 show.html

        } else {
            // 登录失败，可以重定向回登录页面，并给出错误提示
            // return "login"; // 登录失败后重定向到登录页，并添加错误参数
            return "loginPage";
        }








    }











}
